﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Api.Shared.Model.XinYa;
using SqlSugar;

namespace Api.Shared.Model.Dto
{
    [SugarTable("TestReport")]
    public class TestReportDto
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }

        [SugarColumn(IsNullable = true)]
        public DateTime ReportTime { get; set; } = DateTime.Now;

        [SugarColumn(IsNullable = true)]
        public string Operator { get; set; } = string.Empty;

        [SugarColumn(IsNullable = true)]
        public string ProductName { get; set; } = string.Empty;

        [SugarColumn(IsNullable = true)]
        public string ModelName { get; set; } = string.Empty;

        [SugarColumn(IsNullable = true)]
        public string SerialNumber { get; set; } = string.Empty;

        [SugarColumn(IsNullable = true)]
        public int TestType { get; set; }

        [SugarColumn(IsNullable = true)]
        [Navigate(NavigateType.OneToMany, nameof(TestRecord.TestReportId))]
        public List<TestRecord>? TestRecords { get; set; }

        [SugarColumn(IsNullable = true)]
        public string? VoltageListStr { get; set; }

        [SugarColumn(IsNullable = true)]
        public string? CurrentListStr { get; set; }

        [SugarColumn(IsNullable = true)]
        public string? PDListStr { get; set; }

        // 将带有 get => 的自动属性改为标准属性实现，避免使用 C# 预览版的“字段关键字”语法
        [SugarColumn(IsIgnore = true)]
        public List<double>? VoltageList
        {
            get
            {
                return string.IsNullOrWhiteSpace(VoltageListStr)
                    ? []
                    : [.. VoltageListStr
                        .Split(',', StringSplitOptions.RemoveEmptyEntries)
                        .Select(ParseDoubleOrDefault)];
            }
        }

        [SugarColumn(IsIgnore = true)]
        public List<double>? CurrentList
        {
            get
            {
                return string.IsNullOrWhiteSpace(CurrentListStr)
                    ? []
                    : CurrentListStr
                        .Split(',', StringSplitOptions.RemoveEmptyEntries)
                        .Select(x => ParseDoubleOrDefault(x))
                        .ToList();
            }
        }

        [SugarColumn(IsIgnore = true)]
        public List<double>? PDList
        {
            get
            {
                return string.IsNullOrWhiteSpace(PDListStr)
                    ? []
                    : PDListStr
                        .Split(',', StringSplitOptions.RemoveEmptyEntries)
                        .Select(ParseDoubleOrDefault)
                        .ToList();
            }
        }

        // 私有静态方法 转换字符串为双精度浮点数
        private static double ParseDoubleOrDefault(string s)
        {
            double result;
            return double.TryParse(s, out result) ? result : 0;
        }
    }
}
